package logic

import (
	"context"
	"fmt"
	"net/http"

	"api/internal/svc"
	"api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type AlipayNotifyLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewAlipayNotifyLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AlipayNotifyLogic {
	return &AlipayNotifyLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *AlipayNotifyLogic) AlipayNotify(w http.ResponseWriter, r *http.Request) (resp *types.AlioayNotifyResponce, err error) {
	// todo: add your logic here and delete this line
	fmt.Println("触发支付宝回调")
	fmt.Println(r)

	fmt.Println(r.Form.Get("trade_status"))
	fmt.Println(r.Form.Get("out_trade_no"))
	trade_status := r.Form.Get("trade_status")
	orderSn := r.Form.Get("out_trade_no")
	fmt.Println(orderSn)
	if trade_status == "TRADE_CLOSED" {
		//调用修改订单服务
		//扣减商品库存
	}
	//交易创建，等待买家付款。
	if r.FormValue("trade_status") == "WAIT_BUYER_PAY" {
	}
	//回复支付宝 suceess
	w.Write([]byte("success"))
	return &types.AlioayNotifyResponce{}, nil
}
